package com.brainhealth.web.controller.customer;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.brainhealth.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.brainhealth.common.annotation.Log;
import com.brainhealth.common.core.controller.BaseController;
import com.brainhealth.common.core.domain.AjaxResult;
import com.brainhealth.common.enums.BusinessType;
import com.brainhealth.customer.domain.CustomerLearn;
import com.brainhealth.customer.service.ICustomerLearnService;
import com.brainhealth.common.utils.poi.ExcelUtil;
import com.brainhealth.common.core.page.TableDataInfo;

/**
 * 会员学习记录Controller
 * 
 * @author wolong
 * @date 2023-05-06
 */
@RestController
@RequestMapping("/customer/customerLearn")
public class CustomerLearnController extends BaseController
{
    @Autowired
    private ICustomerLearnService customerLearnService;

    /**
     * 查询会员学习记录列表
     */
    @PreAuthorize("@ss.hasPermi('customer:customerLearn:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerLearn customerLearn)
    {
        startPage();
        List<CustomerLearn> list = customerLearnService.selectCustomerLearnList(customerLearn);
        return getDataTable(list);
    }

    /**
     * 导出会员学习记录列表
     */
    @PreAuthorize("@ss.hasPermi('customer:customerLearn:export')")
    @Log(title = "会员学习记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerLearn customerLearn)
    {
        List<CustomerLearn> list = customerLearnService.selectCustomerLearnList(customerLearn);
        ExcelUtil<CustomerLearn> util = new ExcelUtil<CustomerLearn>(CustomerLearn.class);
        util.exportExcel(response, list, "会员学习记录数据");
    }

    /**
     * 获取会员学习记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:customerLearn:query')")
    @GetMapping(value = "/{customerLearnId}")
    public AjaxResult getInfo(@PathVariable("customerLearnId") Long customerLearnId)
    {
        return success(customerLearnService.selectCustomerLearnByCustomerLearnId(customerLearnId));
    }

    /**
     * 新增会员学习记录
     */
    @PreAuthorize("@ss.hasPermi('customer:customerLearn:add')")
    @Log(title = "会员学习记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerLearn customerLearn)
    {
        return toAjax(customerLearnService.insertCustomerLearn(customerLearn));
    }

    /**
     * 修改会员学习记录
     */
    @PreAuthorize("@ss.hasPermi('customer:customerLearn:edit')")
    @Log(title = "会员学习记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerLearn customerLearn)
    {
        return toAjax(customerLearnService.updateCustomerLearn(customerLearn));
    }

    /**
     * 删除会员学习记录
     */
    @PreAuthorize("@ss.hasPermi('customer:customerLearn:remove')")
    @Log(title = "会员学习记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerLearnIds}")
    public AjaxResult remove(@PathVariable Long[] customerLearnIds)
    {
        return toAjax(customerLearnService.deleteCustomerLearnByCustomerLearnIds(customerLearnIds));
    }

    /**
     * 添加学习库学习记录
     * @param request
     * @param customerLearn
     * @return
     */
    @Anonymous
    @PostMapping("/addCustomerLearn")
    public AjaxResult addCustomerLearn(HttpServletRequest request, @RequestBody CustomerLearn customerLearn)
    {
        boolean result = customerLearnService.addCustomerLearn(request, customerLearn);
        if (result)
        {
            return AjaxResult.success("添加学习库学习记录成功！");
        }
        return AjaxResult.error("添加学习库记录失败或已观看过！");
    }
}
